package com.qyhl.school.school.vlog.play;

import android.util.Log;

import com.qyhl.school.common.SchoolUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.school.SchoolThemeBean;
import com.qyhl.webtv.commonlib.entity.school.SchoolVlogBean;
import com.qyhl.webtv.commonlib.entity.school.SchoolVlogThemeDetailBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

public class SchoolVlogPlayModel implements SchoolVlogPlayContract.SchoolVlogPlayModel {
    private SchoolVlogPlayPresenter mPresenter;

    public SchoolVlogPlayModel(SchoolVlogPlayPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(int vlogId, String username) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(SchoolUrl.BASE_SCHOOL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.SCHOOL_VERSION);
            object.put("name", "vlogDetail");
            object.put("vlogId", vlogId);
            object.put("username", username);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<SchoolVlogBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setError("获取详情数据失败！");
                    }

                    @Override
                    public void onSuccess(SchoolVlogBean bean) {
                        if (bean != null) {
                            mPresenter.setDetail(bean);
                        } else {
                            mPresenter.setError("暂无详情！");
                        }
                    }
                });
    }

    @Override
    public void doPraise(int vlogId, String username) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(SchoolUrl.BASE_SCHOOL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.SCHOOL_VERSION);
            object.put("name", "likeVlog");
            object.put("vlogId", vlogId);
            object.put("username", username);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setPraiseError("点赞失败！");
                    }

                    @Override
                    public void onSuccess(String bean) {
                        mPresenter.setPraiseSuccess(true, "点赞成功");
                    }
                });
    }

    @Override
    public void cancelPraise(int vlogId, String username) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(SchoolUrl.BASE_SCHOOL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.SCHOOL_VERSION);
            object.put("name", "cancleLikeVlog");
            object.put("vlogId", vlogId);
            object.put("username", username);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setPraiseError("取消失败！");
                    }

                    @Override
                    public void onSuccess(String bean) {
                        mPresenter.setPraiseSuccess(false, "取消成功");
                    }
                });

    }

    @Override
    public void getData(int tagId, int lastId, int lastBoost) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(SchoolUrl.BASE_SCHOOL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.SCHOOL_VERSION);
            object.put("lastId", lastId);
            object.put("lastBoost", lastBoost);
            if (tagId == 0) {
                object.put("name", "vlogHomePage");
            } else {
                object.put("name", "tagHomePage");
                object.put("tagId", tagId);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        if (tagId == 0) {
            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<List<SchoolVlogBean>>() {
                        @Override
                        public void onError(ApiException e) {
                            mPresenter.setDataError("获取失败！");
                        }

                        @Override
                        public void onSuccess(List<SchoolVlogBean> beanList) {
                            if (beanList != null && beanList.size() > 0) {
                                mPresenter.setData(beanList);
                            } else {
                                mPresenter.setDataError("暂无更多内容！");
                            }
                        }
                    });
        } else {
            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<SchoolVlogThemeDetailBean>() {
                        @Override
                        public void onError(ApiException e) {
                            mPresenter.setDataError("获取失败！");
                        }

                        @Override
                        public void onSuccess(SchoolVlogThemeDetailBean bean) {
                            if (bean != null) {
                                SchoolThemeBean themeBean = new SchoolThemeBean(bean.getImgUrl(), bean.getName(), bean.getInfo(), bean.getId());
                                if (bean.getVlogList() != null && bean.getVlogList().size() > 0) {
                                    for (int i = 0; i < bean.getVlogList().size(); i++) {
                                        bean.getVlogList().get(i).setTag(themeBean);
                                    }
                                    mPresenter.setData(bean.getVlogList());
                                } else {
                                    mPresenter.setDataError("暂无更多内容！");
                                }
                            } else {
                                mPresenter.setDataError("暂无更多内容！");
                            }
                        }
                    });
        }
    }

    @Override
    public void doShare(int vlogId) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(SchoolUrl.BASE_SCHOOL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.SCHOOL_VERSION);
            object.put("name", "vlogShare");
            object.put("vlogId", vlogId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        Log.i("vlog", "分享失败");
                    }

                    @Override
                    public void onSuccess(String bean) {
                        mPresenter.shareSuccess();
                    }
                });
    }
}
